package cn.sx.yys.support;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用的返回结果
 *
 * @author 猿帅
 * @email 1603194187@qq.com
 * @date 2021/1/29 15:13
 */
@Accessors(chain = true)
@Data
public class RequestResult implements Serializable {
    /**
     * 请求结果的状态码
     * 0,成功
     * 其他,失败
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String message;

    /**
     * 返回数据
     */
    private Map<String, Object> data;

    public static RequestResult success(){
       return new RequestResult().setCode(200).setMessage("请求成功");
    }

    public static RequestResult success(Integer code,String message){
        return new RequestResult().setCode(code).setMessage(message);
    }

    public static RequestResult error(Integer code,String message){
        return new RequestResult().setCode(code).setMessage(message);
    }

    public static RequestResult error(int code){
        if(code==ErrorCode.LoginError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.LoginError.getText());
        }
        if(code==ErrorCode.ParamError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.ParamError.getText());
        }
        if(code==ErrorCode.IdError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.IdError.getText());
        }
        if(code==ErrorCode.AuthError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.AuthError.getText());
        }
        if(code==ErrorCode.UserDisable.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.UserDisable.getText());
        }
        if(code==ErrorCode.PhoneError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.PhoneError.getText());
        }
        if(code==ErrorCode.EmailError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.EmailError.getText());
        }
        if(code==ErrorCode.CodeError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.CodeError.getText());
        }
        if(code==ErrorCode.FormError.getCode()){
            return new RequestResult().setCode(code).setMessage(ErrorCode.FormError.getText());
        }
        return RequestResult.error(500,"请求失败");
    }

    public RequestResult() {
        this.code = 200;
        this.data = new HashMap<>();
    }

    public RequestResult(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.message = errorCode.getText();
    }

    public RequestResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public void appendData(String kay, Object value){
        if(this.data == null){
            this.data = new HashMap<>();
        }
        this.data.put(kay, value);
    }
}
